package com.example.tvrecyclerview.fragment;

import android.view.View;
import android.widget.ImageView;

import com.example.tvrecyclerview.R;
import com.example.tvrecyclerview.view.CustomProgressBar;
import com.example.tvrecyclerview.util.FocusViewUtils;
import com.example.tvrecyclerview.view.OnProgressBarListener;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: njb
 * @date: 2020/9/24 0024 0:47
 * @desc:
 */
public class OtherFragment extends FragmentBase implements OnProgressBarListener {
    private static final float scale0 = 1f;
    private static final float scaleL = 1.12f;
    private static final float scaleS = 1.22f;
    private static final float z1 = 0f;
    private static final float z2 = 10f;
    private static final long animateDuration = 20;
    private static View lastFocus;
    private ImageView ivRecentControl, ivCollectControl, ivRemoteControl, ivHandleControl, ivQrCodeControl;
    private Timer timer;
    private CustomProgressBar progressBar;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_other;
    }

    @Override
    protected void rootViewNoNull() {
        super.rootViewNoNull();
        if(null != lastFocus){
            lastFocus.requestFocus();
        }
    }

    @Override
    public void setCacheViewFromContent(View focusView) {
        if(null != lastFocus){
            lastFocus.requestFocus();
        }
    }

    @Override
    public void setmCacheViewFromTab(View focusView) {
        if(null != lastFocus){
            lastFocus.requestFocus();
        }
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        //初始化控件
        ivRecentControl = rootView.findViewById(R.id.iv_my_recent);
        ivCollectControl = rootView.findViewById(R.id.iv_my_collect);
        ivHandleControl = rootView.findViewById(R.id.iv_my_handle);
        ivRemoteControl = rootView.findViewById(R.id.iv_my_remote);
        ivQrCodeControl = rootView.findViewById(R.id.iv_my_qrcode);
        progressBar = rootView.findViewById(R.id.numberbar);
        //传入需要放大缩小的焦点状态view和放大倍数类型
        FocusViewUtils.scaleView(ivRecentControl,0);
        FocusViewUtils.scaleView(ivCollectControl,0);
        FocusViewUtils.scaleView(ivHandleControl,1);
        FocusViewUtils.scaleView(ivRemoteControl,1);
        FocusViewUtils.scaleView(ivQrCodeControl,1);
        initProgress();
    }

    /**
     * 初始化进度条
     */
    private void initProgress() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
              getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progressBar.incrementProgressBy(1);
                    }
                });
            }
        }, 100, 100);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != timer) {
            timer.cancel();
        }
    }

    @Override
    public void onProgressChange(int progress, int max) {
        if (progress == max) {
            progressBar.setProgress(0);
        }
    }

}
